﻿<?php
ini_set("display_errors","On");
		ob_start();
		header ( "content-type:   application/excel" );
		
?>
<?php require_once("./include/db_info.inc.php");
global $mark_base,$mark_per_problem,$mark_per_punish;
 $mark_start=60;
 $mark_end=100;
 $mark_sigma=10;
if(isset($OJ_LANG)){
		require_once("./lang/$OJ_LANG.php");
}
if (!(isset($_SESSION['administrator'])||
			isset($_SESSION['contest_creator'])||
			isset($_SESSION['problem_editor'])))
			{
				echo "<a href='./contest.php'>No Permission!</a>";
				exit(1);
			}
require_once("./include/const.inc.php");
require_once("./include/my_func.inc.php");
class TM
{
    var $solved = 0;
    var $time = 0;
    var $p_wa_num;
    var $p_ac_sec;
    var $user_id;
    var $nick;
    var $school;
    function TM()
    {
        $this->solved   = 0;
        $this->time     = 0;
        $this->p_wa_num = array(
                              0
                          );
        $this->school		= "";                  
        $this->p_ac_sec = array(
                              0
                          );
    }
    function Add($pid, $sec, $res)
    {
        //              echo "Add $pid $sec $res<br>";
        if (isset($this->p_ac_sec[$pid]) && $this->p_ac_sec[$pid] > 0)
            return;
        if ($res != 4)
        {
            if (isset($this->p_wa_num[$pid]))
            {
                $this->p_wa_num[$pid]++;
            }
            else
            {
                $this->p_wa_num[$pid] = 1;
            }
        }
        else
        {
            $this->p_ac_sec[$pid] = $sec;
            $this->solved++;
            if (!isset($this->p_wa_num[$pid]))
                $this->p_wa_num[$pid] = 0;
            $this->time += $sec + $this->p_wa_num[$pid] * 1200;
            //                      echo "Time:".$this->time."<br>";
            //                      echo "Solved:".$this->solved."<br>";
        }
    }
}

function s_cmp($A, $B)
{
    //      echo "Cmp....<br>";
    if ($A->school != $B->school)
    		return $A->school < $B->school;
    else if ($A->solved != $B->solved)
        return $A->solved < $B->solved;
    else
        return $A->time > $B->time;
}
function sch_cmp($A, $B)
{
    //      echo "Cmp....<br>";
    if ($A->solved != $B->solved)
        return $A->solved < $B->solved;
    else
        return $A->time > $B->time;
}

function normalDistribution( $x,  $u,  $s) {

		$ret = 1 / ($s * sqrt(2 *  M_PI))
				* pow( M_E, -pow($x - $u, 2) / (2 * $s * $s));

		return $ret;

	}
// contest start time
if (!isset($_GET['cid'])) die("No Such Contest!");
$cid=intval($_GET['cid']);
//require_once("contest-header.php");
$sql="SELECT `start_time`,`title` FROM `contest` WHERE `contest_id`='$cid'";
$result=mysql_query($sql) or die(mysql_error());
$rows_cnt=mysql_num_rows($result);
$start_time=0;
if ($rows_cnt>0){
	$row=mysql_fetch_array($result);
	$start_time=strtotime($row[0]);
	$title=$row[1];
	if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')){
		$title=iconv("utf8","gbk",$title);
	}
	header ( "content-disposition:   attachment;   filename=contest".$cid."_".$title.".xls" );
}
mysql_free_result($result);
if ($start_time==0){
	echo "No Such Contest";
	//require_once("oj-footer.php");
	exit(0);
}

if ($start_time>time()){
	echo "Contest Not Started!";
	//require_once("oj-footer.php");
	exit(0);
}

$sql="SELECT count(1) FROM `contest_problem` WHERE `contest_id`='$cid'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$pid_cnt=intval($row[0]);
if($pid_cnt==1) {
	$mark_base=100;
	$mark_per_problem=0;
}else{
	$mark_per_problem=(100-$mark_base)/($pid_cnt-1);
}
$mark_per_punish=$mark_per_problem/5;
mysql_free_result($result);

$sql="SELECT 
	users.user_id,users.nick,users.school,users.email,solution.result,solution.num,solution.in_date 
		FROM 
			(select * from solution where solution.contest_id='$cid' and num>=0) solution 
		left join users 
		on users.user_id=solution.user_id 
	ORDER BY users.user_id,in_date";
//echo $sql;
$result=mysql_query($sql);
$user_cnt=0;
$user_name='';
$U=array();

while ($row=mysql_fetch_object($result)){
	
	if($chebool && (!isset($cheschok[$row->school]) || $cheschok[$row->school] != true)) continue;
	
	$n_user=$row->user_id;
	if (strcmp($user_name,$n_user)){
		$user_cnt++;
		$U[$user_cnt]=new TM();
		$U[$user_cnt]->user_id=$row->user_id;
    $U[$user_cnt]->nick=$row->nick;
    $U[$user_cnt]->school=$row->school;
    $U[$user_cnt]->email=$row->email;
		$user_name=$n_user;
	}
	$U[$user_cnt]->Add($row->num,strtotime($row->in_date)-$start_time,intval($row->result));
}
mysql_free_result($result);
usort($U,"s_cmp");

//计算school排名表
$lastsch = "#@!";
$scnt = 0;
$schcnt = 0;
$lin = 0;
$schor         = array();
for ($i = 0; $i < $user_cnt; $i++)
{
	if($U[$i]->nick[0]!="*")
	{
		if($U[$i]->school != $lastsch)
		{
			$lastsch = $U[$i]->school;
			$scnt = 1;
			$schor[] = new TM();
	    $schor[$schcnt]->school  = $U[$i]->school;
	    $schor[$schcnt]->solved  = $U[$i]->solved;
	    $schor[$schcnt]->time  = $U[$i]->time;
	    for($j = 0; $j < $pid_cnt; $j ++)
	    {
	    	if($first_blood[$j] == $U[$i]->nick)
	    		$schor[$schcnt]->nick = $U[$i]->nick;
	    	if(!isset($schor[$schcnt]->p_ac_sec[$j]))
	    		$schor[$schcnt]->p_ac_sec[$j] = 0;
	    	$schor[$schcnt]->p_ac_sec[$j] += isset($U[$i]->p_ac_sec[$j]) && $U[$i]->p_ac_sec[$j] > 0;
	    	if(!isset($schor[$schcnt]->p_wa_num[$j]))
	    		$schor[$schcnt]->p_wa_num[$j] = 0;
	    	$schor[$schcnt]->p_wa_num[$j] += isset($U[$i]->p_wa_num[$j]) && $U[$i]->p_wa_num[$j] > 0;
	  	}
			$schcnt ++;
		//	echo $U[$i]->school;
	  }
	  else if($scnt < $TOPSCHOOLNUM)
	  {
	    $lin = $schcnt - 1;
	    $schor[$lin]->solved  += $U[$i]->solved;
	    $schor[$lin]->time  += $U[$i]->time;
	    for($j = 0; $j < $pid_cnt; $j ++)
	    {
	    	if($first_blood[$j] == $U[$i]->nick)
	    		$schor[$lin]->nick = $U[$i]->nick;
	    	if(!isset($schor[$lin]->p_ac_sec[$j]))
	    		$schor[$lin]->p_ac_sec[$j] = 0;
	    	$schor[$lin]->p_ac_sec[$j] += isset($U[$i]->p_ac_sec[$j]);
	    	if(!isset($schor[$lin]->p_wa_num[$j]))
	    		$schor[$lin]->p_wa_num[$j] = 0;
	    	$schor[$lin]->p_wa_num[$j] += isset($U[$i]->p_wa_num[$j]);
	  	}
	  	$scnt ++;
		}
	}
}
usort($schor, "sch_cmp");

$rank=1;
echo "<center><h3>School RankList -- $title</h3></center>";
echo "<table border=1><tr><td>Rank<td>School<td>Solved<td>Penalty";
for ($i=0;$i<$pid_cnt;$i++)
	echo "<td>$PID[$i]";
echo "</tr>";

for ($i = 0; $i < $schcnt; $i++)
{
	if ($i&1) echo "<tr class=oddrow align=center>";
	else echo "<tr class=evenrow align=center>";
	echo "<td>";
	 
  $sschool = $schor[$i]->school;
  echo $rank++;
  $usolved = $schor[$i]->solved;
	if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')){
		$sschool=iconv("utf8","gbk",$sschool);
	}
	echo "<td>".$sschool."";
	echo "<td>$usolved";
  echo "<td>".sec2str($schor[$i]->time);
	
	 for ($j = 0; $j < $pid_cnt; $j++)
    {
        echo "<td class=well style='padding:1px;'>";
        if(isset($schor[$i]))
        {
            if (isset($schor[$i]->p_ac_sec[$j]) && $schor[$i]->p_ac_sec[$j] > 0)
                echo $schor[$i]->p_ac_sec[$j];
            if (isset($schor[$i]->p_wa_num[$j]) && $schor[$i]->p_wa_num[$j] > 0)
                echo "(-".$schor[$i]->p_wa_num[$j].")";
        }
    }
	echo "</tr>";
}
echo "</table>";

?>
